package com.richard.demo.controller;

import com.richard.demo.chat.ISparkChatCallBack;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CountDownLatch;

@RestController
public class ChatDemo {
    @PostMapping(value = "/chatgpt", produces = {"application/json;charset=UTF-8"})
    public Map enter(@RequestBody Map<String, String> param) throws Exception {   //json{"键1":值1,"键2"：值2}
        String question = param.get("question");
        System.out.println(question);
        final String[] result = {""};//数组中的元素可以被内部类访问
        CountDownLatch countDownLatch = new CountDownLatch(1);
        BigModelNew.chat(question, new ISparkChatCallBack() { //接口的匿名实现类
            @Override
            public void answer(String msg) {
                //这个位置返回大模型的回复数据
                System.out.println("main answer:" + msg);
                result[0] = msg;
                countDownLatch.countDown();
            }
        });
        countDownLatch.await();//会等待，等子线程把倒计时数完
        Map map = new HashMap();
        map.put("code", 0);
        map.put("answer", result[0]);
        return map;//主线程没有等待子线程的执行结果，直接就返回了
    }
}












